package cc.chinagps.gateway.db;

import cc.chinagps.gateway.util.IOUtil;
import cc.chinagps.gateway.util.SystemConfig;
import cc.chinagps.gateway.util.Util;
import com.mchange.v2.c3p0.ComboPooledDataSource;

import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

public class ConnectionManager {
    private static ComboPooledDataSource pool;
    
	static{
		pool = new ComboPooledDataSource();
		Properties properties = SystemConfig.getDBProperties();
		Util.initObjectByProperties(pool, properties);
	}
	
	public static Connection openConnection() throws SQLException {
		Connection con = pool.getConnection();
		con.setAutoCommit(true);
		return con;
	}
	
	public static void checkDBConnection(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		System.out.println("[" + sdf.format(new Date()) + "]checkDBConnection start");
		
		
		Connection con = null;
		try{
			con = ConnectionManager.openConnection();
			System.out.println("[" + sdf.format(new Date()) + "]checkDBConnection ok");
		}catch (Throwable e) {
			e.printStackTrace();
		}finally{
			IOUtil.closeDB(null, null, con);
		}
	}
}